<template>
	<!-- pages/workbench/materialManagement/materialControl/index -->
	<!-- 物资总控 -->
	<view>
		<u-loading-page bg-color="#fff" :loading="pageLoading" textSize="8"></u-loading-page>
		<view class="container-box" v-if="!pageLoading">
			<view class="pl-30">
				<sub-title>基本信息</sub-title>
			</view>
			<view class="pl-30 pr-30">
				<u--form labelPosition="left" :model="formData" class="pl-30 pr-30" :labelStyle="{fontSize: '28rpx'}"
					ref="baseInfoForm" :rules="rules2">
					<u-form-item v-for="(item, i) in mapData" :key="i" :label="item.name" :borderBottom="true"
						:labelWidth="95" @click="taxRateShow(['增值税税率'].indexOf(item[0])!=-1)">
						<u--input :value="formData[item.label]" border="none" inputAlign="right" readonly>
						</u--input>
						<u-icon v-if="['增值税税率'].indexOf(item[0])!=-1" slot="right" name="arrow-right"></u-icon>
					</u-form-item>
				</u--form>
			</view>

			<view class="space" />
			<view class="pl-30 pt-30">
				<sub-title>清单明细</sub-title>
			</view>
			<view class="ml-30 mr-30 mb-30 mt-30 collapse-box">
				<u-collapse accordion :border="false" value="0">
					<u-collapse-item :name="i" title="物资名称" v-for="(item, i) in listData" :key="i" class="mb-20">
						<text slot="value" class="u-page__item__title__slot-title">{{item.materialNameDictLabel}}</text>
						<u--form labelPosition="left" labelWidth="160" :labelStyle="{fontSize: '28rpx'}">
							<u-form-item v-for="(it, ind) in column" :label="it.label"
								:borderBottom="ind==column.length-1?false:true" :labelWidth="it.label!='备注'?'160':'80'"
								:key="ind">
								<u--input v-if="it.label!='备注'" :value="item[it.key]" border="none" inputAlign="right" readonly>
								</u--input>
								<u--textarea v-else confirmType="done" @linechange="lineChange2" :autoHeight="true"
									:class="{projectclass: true,oneLine: lineCount2==1}" v-model="item[it.key]" border="none">
								</u--textarea>
							</u-form-item>
						</u--form>
					</u-collapse-item>
				</u-collapse>
			</view>

			<!-- 审批 -->
			<view class="space" v-if="fromTab==0" />
			<view class="pl-30 pt-30">
				<sub-title v-if="fromTab==0">审批</sub-title>
			</view>
			<u--form v-if="fromTab==0" :model="approvalForm" ref="approvalForm" :rules="rules" class="approval-form">
				<view class="pl-30 pt-30 pr-30 pb-30">
					<view class="pb-30 fs-28">审批结果</view>
					<view class="pb-30" style="border-bottom: 1px solid #dee2eb;">
						<u-radio-group v-model="approvalForm.type" @change="groupChange" class="group-text" size="24">
							<u-radio shape="circle" label="通过" name="1" />
							<u-radio :customStyle="{marginLeft: '36rpx',fontSize: '20rpx'}" shape="circle" label="退回" name="2"
								v-if="stepActive !== 1" />
							<u-radio :customStyle="{marginLeft: '36rpx'}" shape="circle" label="驳回" name="3" />
						</u-radio-group>
					</view>
				</view>
				<view class="pl-30 pr-30 pb-30" v-if="approvalForm.type==2">
					<view class="pb-30 fs-28">退回节点</view>
					<view class="pb-30" style="border-bottom: 1px solid #dee2eb;">
						<!-- <u--input placeholderStyle="fontSize: 28rpx" fontSize="28rpx" readonly
							@click.native="nodeInputHandler" placeholder="请选择退回节点" border="surround"
							v-model="approvalForm.returnNodeName">
						</u--input> -->
						<view @click="nodeInputHandler"
							style="display: flex;align-items: center;font-size: 28rpx;color: #C8CBD2;border: 1rpx solid #EAEAEA;padding: 10rpx;">
							{{approvalForm.returnNodeName||'请选择退回节点'}}
						</view>
					</view>
				</view>
				<view class="pl-30 pr-30 pb-10">
					<view class="pb-30 fs-28">审批意见</view>
					<u--textarea confirmType="done" v-model="approvalForm.comment" maxlength="255" :count="true"
						placeholder="请输入审批意见"></u--textarea>
				</view>
			</u--form>

			<u-picker :show="showPicker" :closeOnClickOverlay="true" :columns="returnsList" @confirm="confirmPicker"
				@close="closePicker" @cancel="closePicker" keyName="name"></u-picker>
			<!-- 审批记录 -->
			<view class="space" />
			<approval-records class="ml-30 mr-30 pt-30" :flowList="flowList" />
			<!-- 提交审批 -->
			<view class="fix-btn" id="fixB" v-if="fromTab==0">
				<u-button type="primary" size="small" text="提交" color="#2289FF" class="btnB fs-28" @click="submit"
					:loading="loading"></u-button>
			</view>
		</view>
		<!-- 增值税率 -->
		<u-picker :show="showtaxRate" keyName="dictLabel" @confirm="taxRate" :columns="tax_rate"
			@cancel="showtaxRate=false">
		</u-picker>
	</view>
</template>

<script>
	import SubTitle from '@/pagesIndex/component/SubTitle.vue'
	import InfoCard from '@/pagesIndex/component/InfoCard.vue'
	import NoData from "@/components/noData/index.vue"
	import ApprovalRecords from '@/pagesIndex/contractManagement/approvelRecords.vue'
	import repeated from '../mixins/repeated.js'
	export default {
		mixins: [repeated],
		components: {
			SubTitle,
			InfoCard,
			NoData,
			ApprovalRecords
		},
		data() {
			return {
				pageLoading: true,
				mapData: [{
						name: '业务单号',
						label: 'reviewCode'
					},
					{
						name: '项目名称',
						label: 'projectName'
					},
					{
						name: '物资类型',
						label: 'materialTypeDictLabel'
					},
					{
						name: '增值税税率',
						label: 'vatRateDictLabel'
					},
					{
						name: '含税合计',
						label: 'totalHasTax'
					},
				],
				column: [{
						label: '单位',
						key: 'unitDictLabel'
					},
					{
						label: '计划采购数量',
						key: 'planPurchaseQuantity'
					},
					{
						label: '含税单价',
						key: 'singleHasTax'
					}, {
						label: '含税合计',
						key: 'productTotalHasTax'
					},
					{
						label: '备注',
						key: 'remark',
						slotName: 'remark'
					},
				],
				rules2: {
					vatRateDictLabel: [{
						required: true,
						message: '请选择',
						trigger: ['blur', 'change']
					}]
				},
				//获取节点信息
				stepActive: 1,
				showtaxRate: false,
				tax_rate: []
			}
		},
		async onLoad({
			id,
			fromTab,
			taskId
		}) {
			this.fromTab = fromTab
			this.detailId = id
			this.approvalForm.taskId = taskId
			await this.getInfoById(this.detailId)
			// await this.getInfoById(69)
			// this.setTeamInfo()
			//获取发票票类型
			await this.dicts('tax_rate').then(res => {
				this.tax_rate = [res.rows]
			})
			await this.getApprovalRecords()
			if (this.fromTab == 0) {
				this.getReturnList()
			}
		},
		methods: {
			taxRateShow(v) {
				if (v && uni.getStorageSync('userid').join(',').includes('wuzibujingli')) {
					this.showtaxRate = true
				}
			},
			submit() {
				this.$refs.approvalForm.validate().then(valid => {
					if (valid) {
						if (uni.getStorageSync('userid').join(',').includes('wuzibujingli')) {
							if (this.formData.vatRateDictLabel != '') {
								this.$definition.updateMaterialControlBase({
									...this.formData,
									isCirculation: false,
									isDraft: 0,
									productList: this.listData
								}).then(res => {
									if (res.code == 200) {
										switch (this.approvalForm.type) {
											case 1:
											case "1":
												// 通过
												this.confirmComplete();
												break;
											case 2:
											case "2":
												// 退回
												this.confirmReturn();
												break;
											case 3:
											case "3":
												// 驳回
												this.confirmReject();
												break;
										}
									} else {
										uni.showToast({
											icon: 'none',
											title: res.msg,
										})
									}
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: '请选择税率',
								})
							}
						} else {
							switch (this.approvalForm.type) {
								case 1:
								case "1":
									// 通过
									this.confirmComplete();
									break;
								case 2:
								case "2":
									// 退回
									this.confirmReturn();
									break;
								case 3:
								case "3":
									// 驳回
									this.confirmReject();
									break;
							}
						}

					}
				})
			},
			//增值税率确定
			taxRate(v) {
				this.formData.vatRateDictLabel = v.value[0].dictLabel
				this.formData.vatRate = v.value[0].dictValue
				this.showtaxRate = false
			},
			setTeamInfo() {
				this.mapData = new Map();
				this.mapData.set('业务单号', 'reviewCode')
				this.mapData.set('项目名称', 'projectName')
				this.mapData.set('物资类型', 'materialTypeDictLabel')
				this.mapData.set('增值税税率', 'vatRateDictLabel')
				this.mapData.set('含税合计', 'totalHasTax')
			},
			// 获取节点信息
			getApprovalNode(procInsId) {
				this.$definition.getApprovalNode(procInsId).then((res) => {
					if (res.code === 200) {
						this.stepActive = res.data.active
					}
				});
			},
			// 请求字典
			dicts(type) {
				return new Promise((reslve, reject) => {
					this.$api.dict({
						pageNum: 1,
						pageSize: 999999,
						dictType: type
					}).then(res => {
						reslve(res)
					})
				})
			},
			getInfoById(id) {
				return this.$api.queryControlDetailById(id).then(res => {
					//清单明细
					this.listData = res.data.productList
					delete res.data.productList
					//基本信息
					this.formData = res.data
					//获取节点信息
					this.getApprovalNode(this.formData.procInsId)
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	@import '../mixins/style.scss';
</style>